package util;

import java.util.Arrays;
import java.util.Collection;
import java.util.function.Function;

import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder;
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;




public class MatchUtil {

  public static void main(String[] args) {
    testCollectionSizeAndElement(Arrays.asList("E"),1,"E");
  }
  /**
   * 比较集合长度和内容，不比较顺序
   * @param actual 集合
   * @param length 集合长度
   * @param item 包含长度
   */
  public static <T> void testCollectionSizeAndElement(Collection<T>actual ,Integer length,Object... item) {
    assertThat(actual,notNullValue());
    assertThat(actual,iterableWithSize(length));
    assertThat(actual,containsInAnyOrder(item));
  }

  /**
   * 比较集合长度和内容，不比较顺序
   * @param actual 集合
   * @param length 集合长度
   * @param item 包含长度
   */
  public static <T> void testArraySizeAndElement(T[] actual,Integer length,Object... item) {
    assertThat(actual,notNullValue());
    assertThat(actual,arrayWithSize(length));
    assertThat(actual,arrayContainingInAnyOrder(item));
  }

  /**
   * 判断数组的属性
   * 
   * @param actual 集合
   * @param length 集合长度
   * @param extracer 提取属性
   * @param item 包含长度
   */
  public static <T,R> void testArrayProps(T[] actual,Integer length,Function<T,R> extracer,Object... item) {
    assertThat(actual,notNullValue());
    assertThat(actual,arrayWithSize(length));
    assertThat(Arrays.stream(actual).map(extracer).toArray(),arrayContainingInAnyOrder(item));
  }
}
